package com.exam.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Result;
import com.exam.demo.entity.Course;
import com.exam.demo.entity.Teacher;
import com.exam.demo.service.CourseService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Resource
    CourseService courseService;

    //查找页面数据
    @GetMapping
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String courseName) {
        Page<Course> page = courseService.findPage(new Page<>(pageNum, pageSize), courseName);
        return Result.success(page);
    }

    //查找全部课程数据
    @GetMapping("/findAll")
    public Result findPage(@RequestParam(defaultValue = "") String  teacherId){
        if(teacherId.equals("")){
            return Result.success(courseService.list());
        }else{
            QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("teacher_id",teacherId);
            return Result.success(courseService.list(queryWrapper));
        }

    }

    //更改和插入
    @PostMapping("/save")
    public Result insert(@RequestBody Course course) {
        return Result.success(courseService.saveOrUpdate(course));
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(courseService.removeById(id));
    }

    /**
     * 导出接口
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        response = courseService.export(response);
    }

    /**
     * excel 导入
     *
     * @param file
     * @throws Exception
     */
    @PostMapping("/import")
    public Result imp(MultipartFile file) throws Exception {
        boolean b = courseService.imp(file);
        return Result.success(b); //false导入失败
    }




}
